﻿using System;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    /// <summary>
    /// 员工
    /// </summary>
    [Authorize]
    [AutomaticTemplate]
    public class EmployeeController : Controller
    {
        /// <summary>
        /// 入口
        /// </summary>
        /// <param name="containerId">容器Id</param>
        /// <returns></returns>
        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                Model = new EmployeeIndexModel()
                {
                },
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            PagedResult<EmployeeDTO> pageInfo;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                pageInfo = proxy.Channel.GetEmployeePaged(parameters.ToPagingFeature());
            }

            return this.Store(pageInfo.Data, pageInfo.TotalRecords);
        }

        public ActionResult GetEmployees(Guid[] orgids)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetEmployeesInOrg(orgids);

                return this.Store(pageInfo);
            }
        }


        public async Task<ActionResult> Details(int? id, string containerId)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetEmployeeInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }
            var vm = dto.To<EmployeeModel>();


            vm.OrgIdDataSource = await OrgController.OrgTree();
            return new PartialViewResult
            {
                Model = vm,
                ContainerId = containerId,
                RenderMode = containerId.IsNullOrEmpty() ? RenderMode.Auto : RenderMode.AddTo
            };
        }

        public async Task<ActionResult> Create()
        {
            var vm = new EmployeeModel();
            vm.OrgIdDataSource = await OrgController.OrgTree();
            return new PartialViewResult { Model = vm };
        }


        public async Task<ActionResult> Edit(int? id)
        {
            EmployeeDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetEmployeeInfo(id.Value);
            }

            EmployeeModel vm = dto.To<EmployeeModel>();
            if (dto == null)
            {
                vm = new EmployeeModel();
            }
            vm.OrgIdDataSource = await OrgController.OrgTree();

            return new PartialViewResult { Model = vm };
        }


        [System.Web.Mvc.HttpPost]
        public ActionResult Save(EmployeeModel user)
        {
            EmployeeDTO dto = user.To<EmployeeDTO>();

            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.SaveEmployee(dto);

                return this.JsonNet(res);
            }

        }

        [System.Web.Mvc.HttpPost]
        public ActionResult Delete(int[] ids)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                res = proxy.Channel.DeleteEmployee(ids);
            }

            return this.JsonNet(res);
        }
        [Permission("设为用户", 4, "003001004")]
        public ActionResult EmployeeAsUser(int? employeeId)
        {
            UserViewModel model;

            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                model = proxy.Channel.GetUserByKey(employeeId.Value).Data.To<UserViewModel>();
                model.RoleList = proxy.Channel.GetRoles();
            }
            model.UI.PostAction = "AsUser";
            model.Id = employeeId.Value;
            model.EmployeeId = employeeId.Value;
            model.PageTitle = "设为用户";

            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                model.Person = proxy.Channel.GetEmployeeInfo(model.EmployeeId).To<EmployeeModel>();
            }

            return new PartialViewResult()
            {
                //ViewName= "AsUser",
                Model = model
            };
        }

        [Permission("设为用户", 4, "003001004")]
        public ActionResult AsUser(UserViewModel vm, int? employeeId)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.EmployeeAsUser(vm.To<UserDTO>(), employeeId.Value);

                return this.JsonNet(res);
            }
        }

        public ActionResult EmployeeDialog()
        {
            return new Ext.Net.MVC.PartialViewResult
            {
                ViewName = "EmployeeDialog",
                Model = new EmployeeIndexModel()
                {
                }
            };
        }
    }
}
